const express = require("express");
const router = express.Router();
const dbCongif = require('../../config/dbconfig')

//查询用户列表(封装方法)
router.get('/home/swiper', (req, res, next) => {
  // 查列表
  let sql = `select * from app_swiper_table `
  let sqlArr = []
  let callBack = (err,results)=>{
    if (err) {
      console.log('连接失败')
    } else {
      let data = {
        swiperList: results,
        success: true,
        message: '加载成功'
      }
      res.send(data)
    }
  }
  dbCongif.sqlConnect(sql,sqlArr,callBack)
})
router.get('/goods/list', (req, res) => {
  console.log(req.query,556);
  let { pageNum, pageSize, type} = req.query
  // 查列表
  let sql = `select * from goods_table where type like ? limit ?,?`
  let sqlArr = ['%'+type+'%',(pageNum-1)*pageSize,parseInt(pageSize)]
  let sqlTotal = `select count(*) as total from goods_table where type like ? `
  let sqlTotalArr = ['%'+type+'%']
  let callBack = (err,results)=>{
    if (err) {
      console.log('连接失败')
    } else {
      let data = {
        goodsList: results,
        total: total,
        success: true,
        message: '加载成功'
      }
      res.send(data)
    }
  }
  let total = 0
  dbCongif.sqlConnect(sqlTotal,sqlTotalArr,(err,res)=>{
    total = res[0].total
    dbCongif.sqlConnect(sql,sqlArr,callBack)
  })
})
//查询用户获取用户详情
//查询用户列表(封装方法)
router.get('/goods/detail', (req, res) => {
  // 查单个
  let sql = `select * from goods_table where id = '${req.query.id}'`
  let sqlArr = []
  let callBack = (err,results)=>{
    if (err) {
      console.log('连接失败')
    } else {
      let data = {
        data: results[0] || {},
        success: true,
        message: '加载成功'
      }
      res.send(data)
    }
  }
  dbCongif.sqlConnect(sql,sqlArr,callBack)
})

module.exports = router;